﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Corsat.Library.DataManager;
using Corsat.Web.Reports;

namespace Corsat.Web
{
    public partial class CourseAbsenceReport : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {

            if (!Page.IsPostBack)
            {
                List<Library.Item> CourseNames = Corsat.Library.DataManager.Main.GetAllItemsByCategory(Library.DataManager.Category.CourseNames);
                CourseNames.Insert(0, new Library.Item());
                ddlCourseNames.DataSource = CourseNames;
                ddlCourseNames.DataTextField = "Value";
                ddlCourseNames.DataValueField = "Id";
                ddlCourseNames.DataBind();
            }

            // BindReport();
        }
        protected void ddlCourseNames_SelectedIndexChanged(object sender, EventArgs e)
        {
            List<Library.Item> CourseLevels = Corsat.Library.DataManager.Main.GetCourseLevels(Convert.ToInt32(ddlCourseNames.SelectedValue));
            rdoLevel.DataSource = CourseLevels;
            rdoLevel.DataTextField = "Value";
            rdoLevel.DataValueField = "Id";
            rdoLevel.DataBind();
        }

        protected void LnkSearch_Clicked(object sender, EventArgs e)
        {
            //BindReport();

            int courseTitleId, coureLevelId;
            int.TryParse(ddlCourseNames.SelectedValue, out courseTitleId);
            int.TryParse(rdoLevel.SelectedValue, out coureLevelId);
            int courseId = Main.GetCourseId(courseTitleId, coureLevelId);
            if (courseId > 0)
            {
                DataTable absence = Main.GetAbsence(courseId, DateTime.Now.Year);

                CrystalReport1 report = new CrystalReport1();

                DataSet1 ds = new DataSet1();

                ds.DataTable1.Merge(absence);
               // ds.Tables.Add(absence);
                report.SetDataSource(ds);
                //CrystalDecisions.CrystalReports.Engine.TextObject root;
                //root = (CrystalDecisions.CrystalReports.Engine.TextObject)
                //     objRpt.ReportDefinition.ReportObjects["txt_header"];
                //root.Text = "Sample Report By Using Data Table!!";

                // Binding the crystalReportViewer with our report object. 
                CrystalReportViewer1.ReportSource = report;
                CrystalReportViewer1.RefreshReport();
                //CrystalReportViewer1.DataBind();
            }
        }

        private void BindReport()
        {
            int courseTitleId, coureLevelId;
            int.TryParse(ddlCourseNames.SelectedValue, out courseTitleId);
            int.TryParse(rdoLevel.SelectedValue, out coureLevelId);
            int courseId = Main.GetCourseId(courseTitleId, coureLevelId);
            if (courseId > 0)
            {
                DataTable absence = Main.GetAbsence(courseId, DateTime.Now.Year);

                CourseAttendanceReport report = new CourseAttendanceReport();

                //DataSet ds = new DataSet();
                //ds.Tables.Add(absence);
                //report.SetDataSource(absence);
                report.SetParameterValue("@CourseId", courseId);
                report.SetParameterValue("@Year", DateTime.Now.Year);
                //CrystalDecisions.CrystalReports.Engine.TextObject root;
                //root = (CrystalDecisions.CrystalReports.Engine.TextObject)
                //     objRpt.ReportDefinition.ReportObjects["txt_header"];
                //root.Text = "Sample Report By Using Data Table!!";

                // Binding the crystalReportViewer with our report object. 
                CrystalReportViewer1.ReportSource = report;
                //CrystalReportViewer1.DataBind();
            }
        }
    }
}